package screen.component
{
	import com.greensock.TimelineMax;
	import com.greensock.TweenLite;
	import com.greensock.easing.Back;
	import com.greensock.easing.Quart;
	import com.pblabs.engine.PBE;
	import com.pblabs.sound.ISoundHandle;
	import com.pblabs.sound.SoundManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.Timer;
	
	import helper.Resources;
	import helper.ValueObject;
	
	import screen.ManulifeScreen;
	
	public class UI extends MovieClip
	{
		private  var _duration:Number = 0.5;
		public  var sprite_mask:MovieClip;
		public var count_down_timer:Timer;
		public var _tooltip:ToolTipNew;
		public var _tooltip2:ToolTipNew;
		public static var _sound_arr:Array=[];
		private static var _popup_disp_arr:Array=[];
		public function UI()
		{
			super();
			_tooltip = new ToolTipNew();
			_tooltip.hook = true;
			_tooltip.cornerRadius = 10;
			_tooltip.tipWidth = 90; 
			_tooltip.align = "center";
			_tooltip.border = 0x503000;
			_tooltip.borderSize = 2;
			
			_tooltip2 = new ToolTipNew();
			_tooltip2.hook = true;
			_tooltip2.cornerRadius = 10;
			_tooltip2.tipWidth = 100; 
			_tooltip2.align = "left";
			_tooltip2.border = 0x503000;
			_tooltip2.borderSize = 2;
		}
		public function popupInit(disp:MovieClip):void{
			ManulifeScreen.getInstance().addChildAt(disp,0);
			disp.alpha = 0;
			disp.x = ValueObject.WIDTH/2+4;
			disp.y = ValueObject.HEIGHT/2+30;
			disp.scaleX = disp.scaleY = 0.5;
			disp.visible = false;
		}
		public function popupShow(disp:MovieClip):void{
			_popup_disp_arr.push(disp);
			ManulifeScreen.getInstance().setChildIndex(disp,ManulifeScreen.getInstance().numChildren -1);
			disp.visible = true;
			TweenLite.to(disp,_duration,{alpha:1,scaleX:1,scaleY:1,ease:Back.easeOut,onComplete:showComplete});
		}
		private function showComplete():void{
			//volumeSound(ValueObject.CLICK_S,1);
		}
		public function popupHide(disp:MovieClip):void{
			if(!disp.visible) return;
			TweenLite.to(disp,_duration,{scaleX:0.5,scaleY:0.5,alpha:0,onComplete:hidePopupComplete,onCompleteParams:[disp]})
			_popup_disp_arr.pop();
		}
		private function hidePopupComplete(disp:MovieClip):void{
			disp.visible = false;
		}
		public function resizePopup():void{
			for(var i:int=0;i<_popup_disp_arr.length;i++){
				var disp:MovieClip = _popup_disp_arr[i];
				disp.x = ValueObject.WIDTH/2 +4 ;
				disp.y = ValueObject.HEIGHT/2+30;
			}
		}
		public  function buttonImageInit(mc:MovieClip):void{
			mc.mouseChildren = false;
			mc.buttonMode = true;
			mc.gotoAndStop("out");
			mc.addEventListener(MouseEvent.MOUSE_OUT,buttonImageOut);
			mc.addEventListener(MouseEvent.MOUSE_OVER,buttonImageOver);
			mc.addEventListener(MouseEvent.MOUSE_DOWN,buttonImageDown);
			mc.addEventListener(MouseEvent.MOUSE_UP,buttonImageOver);
		}
		public  function buttonImageDestroy(mc:MovieClip):void{
			mc.removeEventListener(MouseEvent.MOUSE_OUT,buttonImageOut);
			mc.removeEventListener(MouseEvent.MOUSE_OVER,buttonImageOver);
			mc.removeEventListener(MouseEvent.MOUSE_DOWN,buttonImageDown);
			mc.removeEventListener(MouseEvent.MOUSE_UP,buttonImageOver);
			mc.parent.removeChild(mc);
			mc = null;
		}
		public function buttonImageEnabel(mc:MovieClip):void{
			mc.buttonMode = true;
			mc.gotoAndStop("out");
			mc.addEventListener(MouseEvent.MOUSE_OUT,buttonImageOut);
			mc.addEventListener(MouseEvent.MOUSE_OVER,buttonImageOver);
			mc.addEventListener(MouseEvent.MOUSE_DOWN,buttonImageDown);
			mc.addEventListener(MouseEvent.MOUSE_UP,buttonImageOver);
		}
		public function buttonImageDisable(mc:MovieClip):void{
			mc.buttonMode = false;
			mc.gotoAndStop("disable");
			mc.removeEventListener(MouseEvent.MOUSE_OUT,buttonImageOut);
			mc.removeEventListener(MouseEvent.MOUSE_OVER,buttonImageOver);
			mc.removeEventListener(MouseEvent.MOUSE_DOWN,buttonImageDown);
			mc.removeEventListener(MouseEvent.MOUSE_UP,buttonImageOver);
		}
		private function buttonImageOut(e:MouseEvent):void{
			var mc:MovieClip = MovieClip(e.target);
			mc.gotoAndStop("out");
		}
		private function buttonImageOver(e:MouseEvent):void{
			
			var mc:MovieClip = MovieClip(e.target);
			if(mc.tooltip){
				_tooltip.show(mc,mc.tooltip);
			}
			if(mc.tooltip2){
				_tooltip2.show(mc,mc.tooltip2);
			}
			mc.gotoAndStop("over");
		}
		private function buttonImageDown(e:MouseEvent):void{
			var mc:MovieClip = MovieClip(e.target);
			mc.gotoAndStop("down");
		}
		
		public function sliderInit(disp:MovieClip,no_of_page:Number,no_of_row:Number,start_x:Number,unit_width:Number):void{
			disp.no_of_page = no_of_page;
			disp.no_of_row = no_of_row;
			disp.start_from = 0;
			disp.start_x = start_x;
			disp.unit_width = unit_width;
			var parent:MovieClip = MovieClip(disp.parent);
			(parent.nextBtn as MovieClip).visible = false;
			(parent.preBtn as MovieClip).visible = false;
		}
		public  function sliderNextClick(disp:MovieClip):void{
			disp.start_from += disp.no_of_page;
			startSlider(disp);
		}
		public function sliderPreClick(disp:MovieClip):void{
			disp.start_from -= disp.no_of_page;
			startSlider(disp);
			
		}
		private function startSlider(disp:MovieClip):void{
			if( disp.numChildren - disp.start_from < 1 )
				disp.start_from -=   disp.no_of_page;
			if( disp.start_from < 0 )
				disp.start_from = 0;
			TweenLite.to( disp,2, { x:disp.start_x - Math.ceil(disp.start_from/disp.no_of_row)*disp.unit_width,ease:Quart.easeOut} );
			updateSliderBtn(disp);
		}
		public function updateSliderContent(no:int):void{
	
		}
		public function updateSliderBtn(disp:MovieClip):void{
			var parent:MovieClip = disp.parent as MovieClip;
			if(disp.numChildren <= disp.no_of_page){
				(parent.nextBtn as MovieClip).visible = false;
				(parent.preBtn as MovieClip).visible = false;
			}else{
				(parent.preBtn as MovieClip).visible = disp.start_from>0?true:false;
				(parent.nextBtn as MovieClip).visible = (disp.numChildren - disp.start_from)> disp.no_of_page ? true:false;
			}
		}
		
	
		public static function addSound(name:String,loop:Number=1):void{
			if(!_sound_arr[name]){
				_sound_arr[name] = PBE.soundManager.stream(Resources.SOUND_FOLDER_PATH + name + '.mp3',"sfx",0.0,loop);
			}
		}
		public static function volumeSound(name:String,value:Number):void{
			if(!PBE.soundManager.getCategoryMuted('sfx')&&_sound_arr[name]){
				ISoundHandle(_sound_arr[name]).volume = value;
			}
		}
		public function stopSound(name:String):void{
			if(!PBE.soundManager.getCategoryMuted('sfx')&&_sound_arr[name]){
				ISoundHandle(_sound_arr[name]).stop();
			}
		}
		public function pauseSound(name:String):void{
			if(!PBE.soundManager.getCategoryMuted('sfx')&&_sound_arr[name]){
				ISoundHandle(_sound_arr[name]).pause();
			}
		}
		public static function resumeSound(name:String):void{
			if(!PBE.soundManager.getCategoryMuted('sfx')&&_sound_arr[name]){
				ISoundHandle(_sound_arr[name]).resume();
			}
		}
		
	}
}